/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.microcity.cleanjava.mvvm.viewmodel;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.Transformations;
import android.arch.lifecycle.ViewModel;

import com.darksider.basemodule.baseentity.Resource;
import com.darksider.basemodule.common.AbsentLiveData;
import com.darksider.basemodule.common.Objects;
import com.darksider.basemodule.util.ToastUtils;
import com.microcity.cleanjava.mvvm.model.api.UserRepository;
import com.microcity.cleanjava.mvvm.model.entry.Result;

import javax.inject.Inject;

//如果想要使用应用级别的Context(Application),可以继承AndroidViewModel 他是ViewModel的子类

public class UserViewModel extends ViewModel {


    private final LiveData<Resource<Result>> user;

    final MutableLiveData<LoginInfo> loginInfo;

    @Inject
    public UserViewModel(UserRepository userRepository) {

        this.loginInfo = new MutableLiveData<>();

        user = Transformations.switchMap(loginInfo, input -> {
            if (input == null) {
                return AbsentLiveData.create();
            } else {
                return userRepository.loadUser(input.tel, input.verCode);
            }
        });
    }

    //重连、刷新、重试、重新请求书据
    public void retry() {
        LoginInfo current = loginInfo.getValue();
        if (current != null && !current.isEmpty()) {
            loginInfo.setValue(current);
        }
    }

    //请求数据改变后请求数据
    public void setLoginInfo(String tel, String verCode) {

        LoginInfo update = new LoginInfo(tel, verCode);
        //为了防止重复请求同一参数的地址,如果要提示的话可以在继承AndroidViewModel去Toast提示
        if (Objects.equals(this.loginInfo.getValue(), update)) {
            ToastUtils.showShort("请不要重复请求");
            return;
        }
        this.loginInfo.setValue(update);
    }

    //获取数据
    public LiveData<Resource<Result>> getUser() {
        return user;
    }

    static class LoginInfo {
        public final String tel;
        public final String verCode;

        LoginInfo(String tel, String verCode) {
            this.tel = tel == null ? null : tel.trim();
            this.verCode = verCode == null ? null : verCode.trim();
        }

        boolean isEmpty() {
            return tel == null || verCode == null || tel.length() == 0 || verCode.length() == 0;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }

            LoginInfo repoId = (LoginInfo) o;

            if (tel != null ? !tel.equals(repoId.tel) : repoId.tel != null) {
                return false;
            }
            return verCode != null ? verCode.equals(repoId.verCode) : repoId.verCode == null;
        }

        @Override
        public int hashCode() {
            int result = tel != null ? tel.hashCode() : 0;
            result = 31 * result + (verCode != null ? verCode.hashCode() : 0);
            return result;
        }
    }


}
